package com.tuju.jetpackfirstdemo.ui.animator;

import androidx.appcompat.app.AppCompatActivity;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.LinearInterpolator;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.tuju.jetpackfirstdemo.R;
import com.tuju.jetpackfirstdemo.utils.DisplayUtil;
import com.tuju.jetpackfirstdemo.utils.DisplayUtil2;

public class AnimatorActivity extends AppCompatActivity {
    public static final String MEIZI_URL = "http://ww2.sinaimg.cn/large/9150e4e5gy1g5gaj3k9u9j206o06omy3.jpg";
    private ImageView iv_image;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_animator);
        iv_image = findViewById(R.id.iv_image);
        Glide.with(this)
                .load(MEIZI_URL)
                .into(iv_image);
    }

    public void rotation(View view) {
        //旋转
        ObjectAnimator rotation = ObjectAnimator.ofFloat(iv_image, "rotation", 0, 360);
        rotation.setDuration(200);
        rotation.setRepeatCount(6);
        rotation.setInterpolator(new LinearInterpolator());
        rotation.start();
    }

    public void alpha(View view) {
        //透明度
        ObjectAnimator alpha = ObjectAnimator.ofFloat(iv_image, "alpha", 0f, 1f);
        alpha.setDuration(1800);
        alpha.start();
    }

    public void scale(View view) {
        //缩放
        ObjectAnimator scaleX = ObjectAnimator.ofFloat(iv_image, "scaleX", 1f, 2f);
        ObjectAnimator scaleY = ObjectAnimator.ofFloat(iv_image, "scaleY", 1f, 2f);
        scaleX.setDuration(1800);
        scaleY.setDuration(1800);
        AnimatorSet set = new AnimatorSet();
        set.playTogether(scaleX, scaleY);
        set.start();
    }

    public void scaleLooper(View view) {
        ScaleAnimation scaleAnimation = new ScaleAnimation(1f,0.9f,1f,0.9f);
        scaleAnimation.setRepeatCount(Animation.INFINITE);
        scaleAnimation.setRepeatMode(Animation.REVERSE);
        scaleAnimation.setDuration(2000);
        scaleAnimation.setInterpolator(new DecelerateInterpolator());
        iv_image.startAnimation(scaleAnimation);
        //缩放
//        ObjectAnimator scaleX = ObjectAnimator.ofFloat(iv_image, "scaleX", 1f, 0.9f);
//        ObjectAnimator scaleY = ObjectAnimator.ofFloat(iv_image, "scaleY", 1f, 0.9f);
//        scaleX.setDuration(1800);
//        scaleY.setDuration(1800);
//        AnimatorSet set = new AnimatorSet();
//        set.playTogether(scaleX, scaleY);
//        set.start();
    }

    public void translation(View view) {
        int width = iv_image.getWidth() / 2;
        int height = iv_image.getHeight() / 2;
        Log.i("yanze", "width: " + width);
        Log.i("yanze", "height: " + height);

        float toX = DisplayUtil2.getScreenWith(this) / 2 - width;
        float toY = DisplayUtil2.getScreenHeight(this) / 2 - height;
        Log.i("yanze", "toX: " + toY + " toY:" + toY);
        //平移
        ObjectAnimator translationX = ObjectAnimator.ofFloat(iv_image, "translationX", 0, toX);
        ObjectAnimator translationY = ObjectAnimator.ofFloat(iv_image, "translationY", 0, toY);
        translationX.setDuration(1800);
        translationY.setDuration(900);
        AnimatorSet set = new AnimatorSet();
        set.setInterpolator(new LinearInterpolator());
        set.playTogether(translationX, translationY);
        set.start();
    }

    public void all(View view) {
        //旋转
        ObjectAnimator rotation = ObjectAnimator.ofFloat(iv_image, "rotation", 0, 360);
        rotation.setDuration(200);
        rotation.setRepeatCount(6);

        //透明度
        ObjectAnimator alpha = ObjectAnimator.ofFloat(iv_image, "alpha", 0f, 1f);
        alpha.setDuration(1800);

        //缩放
        ObjectAnimator scaleX = ObjectAnimator.ofFloat(iv_image, "scaleX", 1f, 2f);
        ObjectAnimator scaleY = ObjectAnimator.ofFloat(iv_image, "scaleY", 1f, 2f);
        scaleX.setDuration(1800);
        scaleY.setDuration(1800);

        //平移
        int width = iv_image.getWidth() / 2;
        int height = iv_image.getHeight() / 2;
        Log.i("yanze", "width: " + width);
        Log.i("yanze", "height: " + height);

        float toX = DisplayUtil2.getScreenWith(this) / 2 - width;
        float toY = DisplayUtil2.getScreenHeight(this) / 2 - height;
        Log.i("yanze", "toX: " + toY + " toY:" + toY);
        //平移
        ObjectAnimator translationX = ObjectAnimator.ofFloat(iv_image, "translationX", 0, toX);
        ObjectAnimator translationY = ObjectAnimator.ofFloat(iv_image, "translationY", 0, toY);
        translationX.setDuration(1800);
        translationY.setDuration(900);

        AnimatorSet set = new AnimatorSet();
        set.setInterpolator(new LinearInterpolator());
        set.playTogether(
                rotation,
                alpha,
                scaleX,
                scaleY,
                translationX,
                translationY);
        set.start();
    }
}
